package cn.com.vastbase.hibernate.sample.namedquery.entity;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

import javax.persistence.*;

@Getter
@Setter
@Entity
@Table(name = "ADDRESS")
@NamedQueries({@NamedQuery(name = "@HQL_GET_ALL_ADDRESS",
        query = "from Address")})
@NamedNativeQueries({@NamedNativeQuery(name = "@SQL_GET_ALL_ADDRESS",
        query = "select emp_id, address_line1, city, zipcode from Address")})

public class Address {
    /**
     * 使用共享主键策略，ID与关联的Employee实体主键一致
     * 通过foreign生成器从employee属性获取主键值
     */
    @Id
    @Column(name = "emp_id", unique = true, nullable = false)
    @GeneratedValue(generator = "gen")
    @GenericGenerator(name = "gen", strategy = "foreign",
            parameters = {@Parameter(name = "property", value = "employee")})
    private long id;

    @Column(name = "address_line1")
    private String addressLine1;

    @Column(name = "zipcode")
    private String zipcode;

    @Column(name = "city")
    private String city;

    /**
     * 关联的员工对象
     * 使用主键连接列建立一对一关系
     * 作为外键关系的维护方
     */
    @OneToOne
    @PrimaryKeyJoinColumn
    private Employee employee;


    @Override
    public String toString() {
        return "AddressLine1= " + addressLine1 + ", City=" + city
                + ", Zipcode=" + zipcode;
    }


}
